﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Regex_test
{
    class MathOperations
    {
        public static double MultiplyByThree(double value)
        {
            return value * 3;
        }
        
       public static double Square(double value)
        {
            return value * value;
        }
    }

    //class Math<T>
    //{
    //    public static void Sort(T[] ts, Func<T, T, bool> func)
    //    {
    //        bool flag = true;
    //        do
    //        {
    //            flag = false;
    //            for (int i = 0; i < ts.Length - 1; i++)
    //            {
    //                if (func(ts[i], ts[i + 1]))
    //                {
    //                    T temp = ts[i];
    //                    ts[i] = ts[i + 1];
    //                    ts[i + 1] = temp;
    //                    flag = true;
    //                }
    //            }
    //        } while (flag);
    //    }
    //}

    class Employee
    {
        public string Name { get; private set; }
        public int Salary { get; private set; }

        public Employee(string name, int salary)
        {
            Name = name;
            Salary = salary;
        }

        public static bool Compare(Employee e1, Employee e2)
        {
            return e1.Salary > e2.Salary;
        }
    }

    class EmployeeModel
    {
        public string Name { get; set; }
    }

    class Program
    {
        // 也可以在一个类中单独写一个泛型函数
        public static void Sort<T>(T[] ts, Func<T, T, bool> func)
        {
            bool flag = true;
            do
            {
                flag = false;
                for (int i = 0; i < ts.Length - 1; i++)
                {
                    // if(func(ts[i], ts[i + 1]))       // 两种方法都可以
                    if (func.Invoke(ts[i],ts[i+1]))
                    {
                        T temp = ts[i];
                        ts[i] = ts[i + 1];
                        ts[i + 1] = temp;
                        flag = true;
                    }
                }
            } while (flag);
        }

        static void Main(string[] args)
        {
            //Employee[] employees = { new Employee("win", 1000), new Employee("kri", 800), new Employee("fsd", 3000), new Employee("fas", 700) };
            ////Math<Employee>.Sort(employees, Employee.Compare);
            //Sort<Employee>(employees, Employee.Compare);
            //foreach(Employee e in employees)
            //{
            //    Console.WriteLine(e.Name+":"+e.Salary);
            //}

            Employee[] employees = { new Employee("win", 1000), 
                                    new Employee("kri", 800), 
                                    new Employee("fsd", 3000), 
                                    new Employee("fas", 700) };
            Employee[] es1 = employees.Where(x => x.Salary < 1000).ToArray();   // 采用扩展方法方式的Linq
            Employee[] es2 = (from e in employees                               // 采用表达式方式的Linq
                              where e.Salary >= 1000
                              select e).ToArray();

            EmployeeModel[] eMs1 = employees.Select(e => new EmployeeModel { Name = e.Name }).ToArray();
            EmployeeModel[] eMs2 = (from e in employees
                                   select new EmployeeModel { Name = e.Name }).ToArray();
        }

        public static void Test1()
        {

        }

        public static void Test2()
        {

        }

    }
}
